class PostService {

    def loadPost(id) {
        def post = new Post();
        if (id) {
            post = Post.get(id)
        }
        return post
    }

    def loadPostByUrlTitle(urlTitle) {
        def post = new Post();
        if (urlTitle) {
            post = Post.findByUrlTitle(urlTitle)
        }
        return post
    }

    def publishedPosts() {
        def allPosts = Post.list(sort: 'lastUpdated', order: 'desc')
        return allPosts.findAll {it.published}
    }

    def unpublishedPosts() {
        def allPosts = Post.list(sort: 'lastUpdated', order: 'desc')
        return allPosts.findAll {!it.published}
    }

    def populateTags(post, tagString) {
        post.tags = new TreeSet()
        tagString.tokenize(" ").each {
            if (!post.hasTag(it)) {
                post.addTag(it)
            }
        }
    }

}